import { Account } from '@/account/entity/account.entity';
import { AccountRole } from '@/account/entity/account.entity';
import { AccountType } from '@/account/entity/accountType.entity';
import { Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
import Decimal from 'decimal.js';
import { Security } from './security.entity';
import { Stage } from './stage.entity';
import { InvestOrder } from './investOrder.entity';
import { decimalTransformer } from './utils';

export enum ProjectStatus {
  OPEN = 'open',
  FULLY_FUNDED = 'fully_funded',
  DISCHARGED = 'discharged',
  UPCOMING = 'upcoming',
  DRAFT = 'draft'
}

@Entity()
export class Project {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 256 })
  name: string;

  @Column({ length: 256, unique: true })
  sn: string;

  @Column({ type: 'text' })
  executiveSummary: string;

  @Column({ type: 'decimal', precision: 20, scale: 4, transformer: decimalTransformer, comment: '项目总额' })
  facilityLimit: Decimal;

  @Column({
    type: 'enum',
    enum: ProjectStatus,
    default: ProjectStatus.DRAFT
  })
  status: ProjectStatus;

  @ManyToOne(() => Account, {
    nullable: true,
  })
  @JoinColumn({ name: 'borrower_id' })
  borrower: Account;

  @Column()
  borrowerId: number;

  @OneToMany(() => Stage, stage => stage.project)
  stages: Stage[];

  @OneToMany(() => InvestOrder, order => order.project)
  investOrders: InvestOrder[];

  @Column({ type: 'json' })
  tmp: object; // 新增字段

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
